/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.grayrabbit.pay.chinapay.ChinaPay;
import org.grayrabbit.pay.chinapay.config.ChinaPayConfigUtil;
import org.grayrabbit.pay.chinapay.config.ChinaPayInfo;
import org.grayrabbit.pay.core.biz.manager.trade.ChinaPayTradeManager;
import org.grayrabbit.pay.core.model.entity.transaction.Trade;
import org.grayrabbit.pay.core.model.enums.TradeEnum;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.grayrabbit.web.struts2.GenericAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 接收退款信息结果
 * 
 * @author wy
 * @version $Id: RefundResultAction.java, v 0.1 2012-6-7 下午8:41:01 wy Exp $
 */
@Actions
public class PayRefundResultAction extends GenericAction {

    /**  serialVersionUID*/
    private static final long   serialVersionUID = -5791168137760497805L;

    private static final Logger LOG              = LoggerFactory
                                                     .getLogger(PayRefundResultAction.class);

    @Autowired
    private ChinaPayTradeManager        chinaPayTradeManager;

    private String              ResponseCode;
    private String              Message;
    private String              MerID;
    private String              ProcessDate;
    private String              SendTime;
    private String              TransType;
    private String              OrderId;
    private String              RefundAmout;
    private String              Status;
    private String              Priv1;
    private String              checkValue;

    @Action("pay_refund_result")
    public String execute() {

        if (!"0".equals(ResponseCode)) {
            LOG.error("REFUND 退款提交数据出错,{}", Message);
        }

        if (!ChinaPayConfigUtil.getChinaPayInfo().getMemId().equals(MerID)) {
            //接收结果，抛出异常
            LOG.info("TRADE 商户号{}非本系统", MerID);
            throw new PayException("ERROR_MERID", "商户号非本系统");
        }
        boolean flag = ChinaPay.verifyAuthToken(MerID + ProcessDate + TransType + OrderId
                                                + RefundAmout + Status + Priv1, checkValue);
        if (!flag) {
            LOG.info("REFUND 接收订单号为{}的退款数据验证出错", OrderId);
            throw new PayException("ERROR_REFUND", "订单号" + OrderId + "退款验证出错");
        }
        Trade trade = new Trade();
        trade.setOrderTradeId(OrderId);
        // trade.setMemo(Priv1);
        if (ChinaPayInfo.REFUND_SUBMIT_SUCCESS.equals(Status)) {
            //系统受理
            trade.setStatusEnum(TradeEnum.REFUND_TERMINAL_ACCEPT);
        } else if (ChinaPayInfo.REFUND_SUCCESS.equals(Status)) {
            //退款成功
            trade.setStatusEnum(TradeEnum.REFUND_SUCCESS);
        } else if (ChinaPayInfo.REFUND_FAIL.equals(Status)) {
            //退款失败
            trade.setStatusEnum(TradeEnum.REFUND_FAIL);
        }
        PayResult result = chinaPayTradeManager.updateChinaPay(trade);
        if (!result.isSuccess()) {
            LOG.info("REFUND 接收订单号为{}的交易后台处理出错,{}", OrderId, result.getResultMsg());
            throw new PayException(result.getResultCode(), result.getResultMsg());
        }
        return renderText("接收成功");
    }

    public String getResponseCode() {
        return ResponseCode;
    }

    public void setResponseCode(String responseCode) {
        ResponseCode = responseCode;
    }

    public String getProcessDate() {
        return ProcessDate;
    }

    public void setProcessDate(String processDate) {
        ProcessDate = processDate;
    }

    public String getSendTime() {
        return SendTime;
    }

    public void setSendTime(String sendTime) {
        SendTime = sendTime;
    }

    public String getTransType() {
        return TransType;
    }

    public void setTransType(String transType) {
        TransType = transType;
    }

    public String getOrderId() {
        return OrderId;
    }

    public void setOrderId(String orderId) {
        OrderId = orderId;
    }

    public String getRefundAmout() {
        return RefundAmout;
    }

    public void setRefundAmout(String refundAmout) {
        RefundAmout = refundAmout;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public String getPriv1() {
        return Priv1;
    }

    public void setPriv1(String priv1) {
        Priv1 = priv1;
    }

    public String getCheckValue() {
        return checkValue;
    }

    public void setCheckValue(String checkValue) {
        this.checkValue = checkValue;
    }

    public String getMerID() {
        return MerID;
    }

    public void setMerID(String merID) {
        MerID = merID;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }

}
